package org.zachary.agent.domain.aggregate_model.user;


import jakarta.persistence.Embeddable;
import lombok.Getter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Getter
@Embeddable
public class EncryptedPassword {
    private String value;

    // 强制使用工厂方法
    private EncryptedPassword(String encryptedValue) {
        this.value = encryptedValue;
    }

    protected EncryptedPassword() {

    }

    // 明文→加密对象（用于注册/改密）
    public static EncryptedPassword fromPlaintext(String plainPassword) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return new EncryptedPassword(encoder.encode(plainPassword));
    }

    // 密文→加密对象（用于数据库加载）
    public static EncryptedPassword fromEncrypted(String encryptedValue) {
        return new EncryptedPassword(encryptedValue);
    }

    // 核心验证逻辑
    public boolean matches(String inputPassword) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder.matches(inputPassword, this.value);
    }
    public boolean doNotMatch(String inputPassword) {
        return !matches(inputPassword);
    }

}